/**
 * 洗浴结账
 */
;(function ($) {
	//入口
	$.fn.spaLedger=function(options){
    	//创建对象
		var ledger = new Ledger(this, options);
		ledger.getDefault();
	}
	
	//构造函数
    var Ledger = function(ele, opt) {   	
    	//选择器对象    	
        this.$element=ele;   
        //默认参数
        this.defaults = { 
        	no:"1930",
        	dataBack:function(){},//数据交互完成回调函数
        	returnBack:function(){},//关闭回调函数
        },        
        //初始化参数
        this.options = $.extend({}, this.defaults, opt);
    }
    
    //属性方法
    Ledger.prototype = {
    	//默认值
    	defaultValue:null,
    	//获取默认数据
    	getDefault:function(oper){
    		var $t=this;
    		var $opt=this.options;
    		var el=this.$element;
    		
    		var param={"ct_brzd":{"no":$opt.no}}; 
    		
    		$.post($.HSAPI_AJAXURL+$.HSAPI_PROCEDURE2_LIST,
                    $.HSAPI_ASKJSONINFO(JSON.stringify(param),'POSOrderPayment_CheckoutDefault','{}'),
					  function(data){
						if(data.RETURN==1){
							$t.defaultValue=data;
							if(oper=="reload")
								$t.init();
							else
								$t.html();
						}else{
							jf_alert(data.MSG);
						}
		    },"json");
    	},
    	//生成样式
    	html:function(){
    		var $t=this;
    		var $opt=this.options;
    		var el=this.$element;
    		
    		el.addClass("modal").attr("tabindex","-2").attr("data-backdrop","static");
    		var html = 	'<div class="modal-dialog" style="min-width:1100px;">'+  
							'<div class="modal-content">'+
								'<div class="modal-header no-padding">'+
									'<div class="table-header">'+
										'<span style="font-size:16px;">'+$.i18n.propJsp("结账")+'</span>'+
									'</div>'+
								'</div>'+
								'<div class="modal-body" style="height:550px;">'+
										'<div id="billList" class="widget-body" style="overflow-y:auto;width:100%;border-left:1px solid #cccccc;">'+
											'<table class="table table-bordered" style="border:0; margin-bottom:0px;">'+ 
												'<tbody style="font-size:16px;">'+                   
													'<tr>'+  
														'<td class="center;" style="border: 0px;">'+$.i18n.propJsp("原消费金额")+'</td>'+    
														'<td class="total_price" style="text-align:right;border: 0px;">0.00</td>'+         
													'</tr>'+
												'</tbody>'+    
											'</table>'+	
											'<div class="tableMoney">'+
												'<div class="money_top">'+ 
													'<div class="money_left_info"></div>'+
													'<table class="table table-striped table-bordered" style="border:0;">'+ 
														'<tbody id="tableMethodTable" style="font-size:16px;">'+                   
															
														'</tbody>'+     
													'</table>'+ 
												'</div>'+	
												'<div class="pay_price"  style="font-size:25px;justify-content: flex-end;display: flex;">'+ 
													'<label style="font-size:25px">'+$.i18n.propJsp("应收")+'</label>'+  
													'<span class="pay_p" style="color:#d12723;font-size: 20px;margin-top: 5px;">￥0.00</span>'+  
												'</div>'+   
											'</div>'+
										'</div>'+
									'</div>'+
									'<div id="jzBtnContent" class="money_center" style="position:relative;margin-top:5px;">'+
									   '<div style="display:flex;">'+
									    '<div id="jz_left" class="widget-box no-padding" style="margin: 0px; width: 472px; height: 102px;">'+
										     '<button id="btnDiscount" class="btn  btn-app btn-sm  btn-grey" style="border:1px solid #FFF;height:48px;width:117px;margin:0px;padding:6px 2px;border-radius: 6px !important;"><span data-i18n="折扣" style="font-size:16px;">折扣</span></button>'+
										     '<button id="btnDiscountzero" class="btn  btn-app btn-sm  btn-grey" style="border:1px solid #FFF;height:48px;width:117px;margin:0px;padding:6px 2px;border-radius: 6px !important;"><span data-i18n="折扣抹零" style="font-size:16px;">折扣抹零</span></button>'+
										     '<button id="btnService" class="btn  btn-app btn-sm  btn-grey" style="border:1px solid #FFF;height:48px;width:117px;margin:0px;padding:6px 2px;border-radius: 6px !important;"><span data-i18n="服务费" style="font-size:16px;">服务费</span></button>'+
										     '<button id="btnMemberCoupon" class="btn  btn-app btn-sm  btn-grey" style="border:1px solid #FFF;height:48px;width:117px;margin:0px;padding:6px 2px;border-radius: 6px !important;"><span data-i18n="会员电子券" style="font-size:16px;">会员电子券</span></button>'+
										     '<button id="btnScanCoupon" class="btn  btn-app btn-sm  btn-grey" style="border:1px solid #FFF;height:48px;width:117px;margin:0px;padding:6px 2px;border-radius: 6px !important;"><span data-i18n="扫码电子券" style="font-size:16px;">扫码电子券</span></button>'+
									    '</div>'+
									    '<div id="jz_center" class="widget-box no-padding" style="margin: 0px; width: 122.667px; height: 102px;">'+
										     '<button id="btnChargeOff" type="button" class="btn  btn-app btn-sm  btn-danger" style="height:48px;width:119.66666666666674px;margin:1px;padding:6px 2px;"><span data-i18n="冲账">冲账</span></button>'+
										     '<button id="btnOther" class="btn  btn-app btn-sm  btn-purple" style="border:1px solid #FFF;height:48px;width:119.66666666666674px;margin:1px;padding:6px 2px;"><span data-i18n="更多">更多</span></button>'+
									    '</div>'+
									    '<div id="jz_right" class="widget-box no-padding" style="margin: 0px; width: 472px; height: 102px;">'+
										     '<button class="btn btn-app btn-sm btn-primary paymenttype" data-code="900" data-paytype="CA" data-text="现金" data-type="CA" id="pay0" style="height:48px;width:115px;margin:1px;padding:6px 2px;"><span data-i18n="现金" style="font-size:16px;">现金</span></button>'+
										     '<button class="btn btn-app btn-sm btn-primary paymenttype" data-code="901" data-paytype="CK" data-text="支票" data-type="CK" id="pay1" style="height:48px;width:115px;margin:1px;padding:6px 2px;"><span data-i18n="支票" style="font-size:16px;">支票</span></button>'+
										     '<button class="btn btn-app btn-sm btn-primary paymenttype" data-code="919" data-paytype="CC" data-text="银联卡" data-type="CC" id="pay2" style="height:48px;width:115px;margin:1px;padding:6px 2px;"><span data-i18n="银联卡" style="font-size:16px;">银联卡</span></button>'+
										     '<button class="btn btn-app btn-sm btn-primary paymenttype" data-code="909" data-paytype="AR" data-text="快支付(AR)" data-type="AR" id="pay3" style="height:48px;width:115px;margin:1px;padding:6px 2px;"><span data-i18n="快支付(AR)" style="font-size:15px;">快支付(AR)</span></button>'+
										     '<button class="btn btn-app btn-sm btn-primary paymenttype" data-code="920" data-paytype="AR" data-text="挂帐(1)" data-type="AR" id="pay4" style="height:48px;width:115px;margin:1px;padding:6px 2px;"><span data-i18n="挂帐(1)" style="font-size:16px;">挂帐(1)</span></button>'+
										     '<button class="btn btn-app btn-sm btn-primary paymenttype" data-code="930" data-paytype="AR" data-text="招待" data-type="FR" id="pay5" style="height:48px;width:115px;margin:1px;padding:6px 2px;"><span data-i18n="招待" style="font-size:16px;">招待</span></button>'+
										     '<button class="btn btn-app btn-sm btn-primary paymenttype" data-code="940" data-paytype="AR" data-text="钻石美食卡" data-type="AP" id="pay6" style="height:48px;width:115px;margin:1px;padding:6px 2px;"><span data-i18n="钻石美食卡" style="font-size:16px;">钻石美食卡</span></button>'+
										     '<button class="btn btn-app btn-sm btn-primary paymenttype" data-code="941" data-paytype="AR" data-text="美食卡" data-type="AP" id="pay7" style="height:48px;width:115px;margin:1px;padding:6px 2px;"><span data-i18n="美食卡" style="font-size:16px;">美食卡</span></button>'+
									    '</div>'+
									   '</div>'+
									  '</div>'+
								'<div class="modal-footer">'+  
//									'<div class="btn-group">'+
//										'<button id="btnCancel" type="button" class="btn btn-sm btn-default">'+
//											'<i class="ace-icon fa fa-times"></i>'+$.i18n.propJsp("返回")+
//										'</button>'+
//									'</div>'+
								'</div>'+
							'</div>'+
						'</div>';
			
    		el.append(html);
    		
    		$t.init(); 
    		
    		//模态完全显示后执行
//    		el.on('shown.bs.modal', function () {  
//    			el.find("#room").focus(); 
//			});
    		//显示模态 
//			el.modal("show"); 
    	},
    	//初始化
    	init:function(){
    		var $t=this;
    		var $opt=this.options;
    		var el=this.$element;
    		
    		el.find("#tableMethodTable").empty(); 
    		$($t.defaultValue.ct_brxsxm).each(function(key,value){   
    			if(value.sort!="0") {
    				var html='<tr>'+  
    							'<td class="center;" style="border: 0px;">'+value.mc_c+'</td>'+  
    							'<td style="text-align:right;border: 0px;">'+value.je_c+'</td>'+         
    						'</tr>';
    				el.find("#tableMethodTable").append(html);
    			} 
    		});
    		el.find(".total_price").text($t.defaultValue.amount[0].je); 
    		el.find(".pay_p").text("￥"+$t.defaultValue.amount[0].balance); 
    		
    		$t.event(); 
    		
    	},
    	//绑定事件
    	event:function(){
    		var $t=this;
    		var $opt=this.options;
    		var el=this.$element;	
    		$t.loadCss("css/cashier_mana/posorder.css");
    		
    		//折扣
			el.find("#btnDiscount").unbind("click").click(function(){
				$(this).modalFrame("destroy");
				$(this).modalFrame("init",function(modalEle){   
					$("#"+modalEle).modal("show"); 
					if(!$.isFunction($().discountDetail)){
						$.ajaxSetup({ async: false, cache: false });
			            $.getScript("js/cashier_mana/jquery.modal.discountDetail.js");
			            $.ajaxSetup({ async: true });
					}
					$("#"+modalEle).discountDetail({
						title:"折扣",//标题
					    no:($opt.no||""),
						returnBack:function(){
							$("#"+modalEle).empty();
							$("#"+modalEle).modal("hide");
							$t.getDefault("reload");
						},
						dataBack:function(goposorder){
							$("#"+modalEle).empty(); 
							$("#"+modalEle).modal("hide");
							$t.getDefault("reload");
						},
						getDefaultBack:function(){
							$("#"+modalEle).empty();
							$("#"+modalEle).modal("hide");
							$t.getDefault("reload");
						}
					});
				});
				
			});
    	},
    	loadCss:function(href){
    		$("head").append("<link>");
	        a =$("head").children(":last");
	        a.attr({
		        rel: "stylesheet",
		        type: "text/css",
		        href: href
	        })
    	}
    }
})(jQuery);